import axios from 'axios'
import Vue from '@/main'

const request = axios.create()

request.interceptors.response.use(response => response, error => {
  console.error(error)

  if (error.response) {
    const { data, status } = error.response
    if (status === 401) {
      if (data.toLowerCase().search('login') > -1) {
        Vue.$notify.warning({
          title: '请先登录'
        })
        Vue.router.replace({
          name: 'Login'
        })
      } else if (data.toLowerCase().search('admin') > -1) {
        Vue.$notify.error({
          title: '本操作需要管理员权限'
        })
        Vue.router.go(-1)
      }
    } else if (status === 400) {
      Vue.$notify.warning({
        title: data
      })
    } else {
      Vue.$notify.error({
        title: error.message
      })
    }
  }

  return Promise.reject(error)
})

// Auth
export function AuthGetCheck () {
  return request.get('/api/auth/')
}

export function AuthPostEnroll (form) {
  return request.post('/api/auth/enroll', form)
}

export function AuthPostLogin (form) {
  return request.post('/api/auth/login', form)
}

export function AuthGetCheckUsername (params) {
  return request.get('/api/auth/username', { params })
}

export function AuthPostUsername (form) {
  return request.post('/api/auth/username', form)
}

// Market
export function MarketGetBooks () {
  return request.get('/api/market/book')
}

export function MarketGetBookDetail (isbn) {
  return request.get(`/api/market/book/${isbn}`)
}

export function MarketPostBookReview (isbn, content) {
  return request.post(`/api/market/book/${isbn}/review`, { content })
}

export function MarketPostBookReviewResp (isbn, reviewid, form) {
  return request.post(`/api/market/book/${isbn}/review/${reviewid}`, form)
}

// Custom
export function CustomPostCart (form) {
  return request.post('/api/custom/cart', { cart: form })
}

export function CustomPostOrder (form) {
  return request.post('/api/custom/order', form)
}

export function CustomGetOrders () {
  return request.get('/api/custom/order')
}

export function CustomGetProfile () {
  return request.get('/api/custom/profile')
}

export function CustomPostProfile (form) {
  return request.post('/api/custom/profile', form)
}

// Admin
export function AdminPutNewBook (form) {
  return request.post('/api/admin/book', form)
}

export function AdminGetStock () {
  return request.get('/api/admin/book')
}

export function AdminDeleteBook (isbn) {
  return request.delete(`/api/admin/book/${isbn}`)
}

export function AdminPostUpdateBook (isbn, form) {
  return request.post(`/api/admin/book/${isbn}`, form)
}

export function AdminGetOrders () {
  return request.get('/api/admin/order')
}

export function AdminGetSalesStat () {
  return request.get('/api/admin/stat/sale')
}

export function AdminGetUsers () {
  return request.get('/api/admin/user')
}

export function AdminDeleteUser (id) {
  return request.delete(`/api/admin/user/${id}`)
}

export function AdminPostUpdateUser (id, form) {
  return request.post(`/api/admin/user/${id}`, form)
}
